function evalPro(str: string) {
  try {
    // eslint-disable-next-line no-new-func
    const func = new Function(`'use strict'; return (${str});`);
    return func();
  }
  catch {
    return null;
  }
}

export function filterInputNum(
  eventVal: string | number,
  type: 'int' | 'float' = 'float',
  maxDecimal = 6,
) {
  let val = String(eventVal);
  val = val.replace(/(^\s*)|(\s*$)/g, '');
  if (val !== '') {
    const cases
        = val.indexOf('0') === 0 && val.length > 1 && val.indexOf('.') !== 1;
    const reg = type === 'int' ? /\D/g : /[^\d.]/g;
    val = val.replace(reg, '');
    if (cases) {
      return '0';
    }
    if (type === 'int') {
      val
          = val.indexOf('0') === 0 && val.length > 1
          ? val.substring(0, val.length - 1)
          : val;
    }
    else {
      const req1 = evalPro(`/\\.{${maxDecimal},}/g`);
      const req2 = evalPro(`/^(\\d?)+(\\.\\d{0,${maxDecimal}})?$/`);
      if (val.indexOf('.') === 0) {
        val = '';
        val = val.replace(/[^$#]/g, '0.');
        val = val.replace(req1, '.');
      }
      else if (!req2.test(val)) {
        val = val.substring(0, val.length - 1);
      }
    }
  }
  return val;
}

export function dateToDayjs(val: any) {
  if (!val)
    return null;
  const date = dayjs(val);
  return date.isValid() ? date : null;
}

export function toBnVal(val: Parameters<typeof bignumber>[0] | undefined) {
  if (!val)
    return null;
  const bn = bignumber(val || 0);
  return bn.isNaN() || bn.lte(0) ? null : bn;
}

export function toDecimalPlaces<T extends ReturnType<typeof toBnVal>>(val: T, isUp = false, decimals = 2): T {
  return val?.decimalPlaces(decimals, isUp ? bignumber.ROUND_UP : bignumber.ROUND_DOWN) as T;
}

export function toBnInt<T extends ReturnType<typeof toBnVal>>(val: T, isUp = false): T {
  return val?.integerValue(isUp ? bignumber.ROUND_UP : bignumber.ROUND_FLOOR) as T;
}

export function toRateText<T extends ReturnType<typeof toBnVal>>(bnV: T, isInt = true) {
  const fn = isInt ? toBnInt : toDecimalPlaces;
  return bnV ? `${fn(bnV.multipliedBy(100)).toFormat()}%` : '';
}

export function getMaxRateText(list: ReturnType<typeof bignumber>[], isInt = true) {
  if (!list.length) {
    return '';
  }
  const max = bignumber.max(...list);
  return max.gt(0) ? toRateText(max, isInt) : '';
}

export function addSecond(val: number) {
  return dayjs().add(val || 0, 'second');
}

export function showWithdrawConfirm(onOk: () => void, bnBalance: ReturnType<typeof bignumber>, bnLockedBalance: ReturnType<typeof bignumber>) {
  if (bnBalance.gt(0) && bnLockedBalance.gt(0) && bnLockedBalance.lt(bnBalance)) {
    antdModal.confirm({
      content: $t('frjpy5mrCpb41lpoQg1Bb'),
      class: 'ant-cover__confirm-modal',
      centered: true,
      zIndex: 999,
      onOk,
    });
    return;
  }
  onOk();
}

export function openUrl(url?: string) {
  if (url) {
    if (typeof Telegram.WebApp?.openLink === 'function') {
      Telegram.WebApp?.openLink?.(url);
    }
    else {
      window.open(url, '_blank');
    }
  }
}
